<template>
	<view>

	</view>
</template>

<script>
	export default {
		name: "my-socket",
		props: {
			liveSetting: {
				default: () => ({}),
				type: Object,
			},
		},
		data() {
			return {
				baseUrl: '',
				cookieKS: '',
				cookieDY: '',
				roomId:'',
				playInteract:'',
				actions:'',
				inter:null,
				msgInfo:{}
			};
		},
		watch: {
			liveSetting: {
				handler(val) {
					if (val) {
						this.roomId = val.roomId
						this.playInteract = val.playInteract
						this.actions = val.actions
					}
				},
				immediate: true,
				deep: true,
			},
		},
		methods: {
			initWs() {
				this.baseUrl = ''
				this.$api.wsUrl().then(res => {
					if (res.code == '200') {
						console.log(res.data, 111);
						switch (this.actions) {
							case '抖音':
								if (res.data.ws_douyin == '') {
									this.$tools.toast('抖音直播间正在维护中，暂时无法使用 ', 2000);
									this.$emit('close')
									return;
								} else {
									this.baseUrl = res.data.ws_douyin
									this.cookieDY = res.data.douyin_cookie
								}
								break;
							case '快手':
								if (res.data.ws_kuaishou == '') {
									this.$tools.toast('快手直播间正在维护中，暂时无法使用 ', 2000);
									this.$emit('close')
									return;
								} else {
									this.baseUrl = res.data.ws_kuaishou
									this.cookieKS = res.data.kuaishou_cookie
								}
								break;
							case 'tiktok':
								if (res.data.ws_kuaishou == '') {
									this.$tools.toast('tiktok直播间正在维护中，暂时无法使用 ', 2000);
									this.$emit('close')
									return;
								} else {
									this.baseUrl = res.data.ws_tiktok + '/' + this.roomId
								}
								break;
							default:
								break;
						}
						if (this.baseUrl != '') {
							this.creatSocket()
						}
					}
				})
			},

			creatSocket() {
				uni.showLoading({
					title: '正在获取直播间'
				});
				uni.connectSocket({
					url: this.baseUrl,
				});
				uni.onSocketOpen((res) => {
					if (this.actions == '抖音' || this.actions == '快手') {
						let data = {
							url: this.actions == '快手' ? 'https://live.kuaishou.com/u/' + this.roomId :
								'https://live.douyin.com/' + this.roomId,
							cookie: this.actions == '快手' ? this.cookieKS : this.cookieDY
						}

						uni.sendSocketMessage({
							data: JSON.stringify(data)
						});
						console.log(11111111111, data);
						this.inter = setInterval(() => {
							uni.sendSocketMessage({
								data: 'ping'
							});
						}, 60000)
					}
				});

				uni.onSocketError((res) => {
					uni.hideLoading()
				});
				uni.onSocketClose((res) => {
					uni.hideLoading()
					if (this.msgInfo.tips == '未开播') {
						this.$tools.toast('检测到该房间号未开播，请检查房间号是否正确！', 2000);
						this.$emit('close')
					}
					if (this.msgInfo.tips == "获取roomid失败'room'") {
						this.$tools.toast('直播间获取失败，请检查房间号是否正确！', 2000);
						this.$emit('close')
					}
					clearInterval(this.inter)
					console.log('WebSocket 已关闭！', res, this.roomId);
					setTimeout(() => {
						if (this.roomId) {
							console.log('重连');
							this.creatSocket()
						}
					}, 100)
				});

				uni.onSocketMessage((res) => {
					uni.hideLoading()
					let data = JSON.parse(res.data)
					console.log('data', data, data.tips);
					this.msgInfo = data
					let scrolling = [];

					if (data.tips == '获取roomid成功' || data.tips == '获取参数成功') {
						this.$tools.toast('直播间获取成功！', 2000);
					}

					if (this.actions == '快手' && data.result?.commentFeeds) {
						data.result.commentFeeds.map(i => {
							scrolling.push({
								name: i.user.userName,
								content: i.content
							})
						})
					} else if (this.actions == '抖音') {
						let obj = {}
						console.log(data.common.method, 11111111);
						switch (data.common.method) {
							case 'WebcastChatMessage':
								obj = {
									name: data.user.nickName,
									content: data.content
								}
								scrolling.push(obj)
								break;
							case 'WebcastMemberMessage':
								obj = {
									name: data.user.nickName,
									content: '来了'
								}
								scrolling.push(obj)
								break;
							case 'WebcastLikeMessage':
								obj = {
									name: data.user.nickName,
									content: '点赞' + data.count + '次'
								}
								scrolling.push(obj)
								break;
							default:
								break;
						}
					}

					console.log('*******', scrolling);
					if (scrolling.length > 0) {
						this.msgArr.push(...scrolling)
						let params = {
							type: 'proceed',
							explain_id: this.activeAudio.explainId || '',
							explain_name: this.activeAudio.explainName || '',
							message_list: this.actions == '快手' ? JSON.stringify(data.result) : JSON.stringify(data)
							// message_list: JSON.stringify(data.result): data.message_list
						}
						this.$api.addLiveData(params).then(res => {
							if (res.code == '200') {
								if (res.data.length > 0 && this.audioList[this.cur].isexplan && res.data[0].audio_url != '' &&
									!this.isplay) {
									console.log('res', res);
									this.isplay = true
									this.audioList.shift()
									this.cur -= 1
									if (this.cur < 0) this.cur = 0
									this.audioList.splice(this.cur + 1, 0, {
										src: res.data[0].audio_url,
										isexplan: false
									})
									// 处理互动音频播放
									let time = this.playInteract * 1000
									let timer = setTimeout(() => {
										console.log('play');
										this.problem = res.data[0].keywords
										this.recover = res.data[0].interact_name
										this.cur++
										this.bgAudioManager.src = this.audioList[this.cur].src;
										this.bgAudioManager.play()
										this.activeAudio = this.audioList[this.cur]
										console.log('index', this.cur);
										console.log('timer', timer);
										clearTimeout(timer)
										console.log('timer1', timer);
									}, time)
									// 处理互动音频播放结束
								}
							}
						})
					}
				});
			},
		}
	}
</script>

<style lang="less">

</style>